#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/10/12 11:49
@desc
"""

import os
import threading
import queue
import re, urllib2

# Number of threads
n_thread = 10
# Create queue
queue = queue.Queue()


class ThreadClass(threading.Thread):
    def __init__(self, queue, num):
        threading.Thread.__init__(self)
        # super(ThreadClass, self).__init__()  # Expired
        # Assign thread working with queue
        self.queue = queue
        self.num = num
        self.check_f = self._write_check_file(num)

    def __del__(self):
        self.check_f.close()

    def _write_check_file(self, num):
        checked_dir = r'D:\iProject\myPython\com\teradata\ippool\get_check'
        file_name = 'check_ip_{}'.format(str(num))
        return open(os.path.join(checked_dir, file_name), 'w+')


    def run(self):
        while True:
            # Get from queue job
            host = self.queue.get()
            print(self.getName() + ":" + host)
            try:
                # print("当前代理IP " + host)
                proxy = urllib2.ProxyHandler({"http": host})
                opener = urllib2.build_opener(proxy, urllib2.HTTPHandler)
                urllib2.install_opener(opener)
                url = "http://www.baidu.com"
                data = urllib2.urlopen(url).read().decode('utf-8', 'ignore')
                print("通过")
                self.check_f.writelines(host)
                self.check_f.flush()
                print("-----------------------------")
            except Exception as err:
                print(err)
                print("-----------------------------")

            # signals to queue job is done
            self.queue.task_done()


# Create number process
for i in range(n_thread):
    t = ThreadClass(queue, i)
    t.setDaemon(True)
    # Start thread
    t.start()

# Read file line by line
hostfile = open(r"D:\iProject\myPython\com\teradata\ippool\get_check\thebigproxylist-18-10-12.txt", "r")
for line in hostfile:
    # Put line to queue
    queue.put(line)
# wait on the queue until everything has been processed
queue.join()
